package com.ycy.chuwenuniapp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.chuwenuniapp.common.Constant;
import com.ycy.chuwenuniapp.common.ResCodeEnum;
import com.ycy.chuwenuniapp.common.ResData;
import com.ycy.chuwenuniapp.dto.GoodsColorDto;
import com.ycy.chuwenuniapp.dto.GoodsSizeDto;
import com.ycy.chuwenuniapp.entity.GoodsColor;
import com.ycy.chuwenuniapp.entity.GoodsCount;
import com.ycy.chuwenuniapp.entity.GoodsSize;
import com.ycy.chuwenuniapp.exception.CustomException;
import com.ycy.chuwenuniapp.mapper.IGoodsColorMapper;
import com.ycy.chuwenuniapp.mapper.IGoodsSizeMapper;
import com.ycy.chuwenuniapp.vo.GoodsColorVo;
import com.ycy.chuwenuniapp.vo.GoodsSizeVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsColorService extends ServiceImpl<IGoodsColorMapper, GoodsColor> {
    //增加或修改
    public Boolean saveGoodsColor(String type, GoodsColor entity) {
        String goodsId = entity.getSizeId();
        String color = entity.getColor();
        if(StrUtil.isBlank(goodsId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品id");
        if(StrUtil.isBlank(color)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失商品颜色");

//        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
//        wrapper.eq("goods_id",goodsId);
//        wrapper.eq("color",color);
//        long sameCount = count(wrapper);
//        if(sameCount != 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该商品已存在相同的颜色: " + color);

        if(Constant.ADD.equals(type)) return save(entity);
        else if(Constant.UPDATE.equals(type)) return updateById(entity);
        else throw new CustomException(ResCodeEnum.BAD_REQUEST,"saveGoodsColor方法的类型值只能是Constant.ADD或Constant.UPDATE");
    }

    //查全部
    public List<GoodsColor> findAllByIds(List<String> colorIdList) {
        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
        wrapper.in("id",colorIdList);
        return list(wrapper);
    }

    //查全部
    public List<GoodsColor> findAll(String goodsId, String color) {
        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(goodsId),"goods_id",goodsId);
        wrapper.eq(StrUtil.isNotBlank(color),"color",color);
        return list(wrapper);
    }

    public List<GoodsColor> findAllByGoodsSizeIds(ArrayList<String> goodsSizeIdList) {
        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
        wrapper.in("size_id",goodsSizeIdList);
        return list(wrapper);
    }

    public Boolean delete(List<String> sizeIdList) {
        QueryWrapper<GoodsColor> wrapper = new QueryWrapper<>();
        wrapper.in(sizeIdList.size() != 0,"size_id",sizeIdList);
        return remove(wrapper);
    }
}
